<template>
  <el-dialog :title="title" :visible.sync="visible" width="950px" append-to-body>
    <el-button size="small" @click="init">开始打印</el-button>
    <template v-if="printEquips.length">
      <el-divider></el-divider>
      选择打印设备：<el-button size="small" @click="beginWork(item.printerPort)" v-for="item in printEquips">{{item.printerName}} 端口：{{item.printerPort}}</el-button>
    </template>
    <div id="pcon">
      <div class="A4" :id='"print"+i' :gs1='d.rfidCode' v-for="(d, i) in assets">
        <div name='资产名称' left='1.30' top='0.30' width='10' white-space='nowap' >资产名称：{{d.assetName}}</div>
        <div name='资产编码' left='1.30' top='0.60' width='10' white-space='nowap' >资产编码：{{d.assetCode}}</div>
        <div name='品牌' left='1.30' top='0.90' width='10' white-space='nowap' >品牌:{{d.brandName}}</div>
        <div name='型号' left='1.30' top='1.20' width='10' white-space='nowap' >型号:{{d.modelName}}</div>
        <div name='使用部门' left='1.30' top='1.50' width='10' white-space='nowap' >使用部门:{{d.deptName}}</div>
        <div name='gs1' left='0.20' top='0.20' height='1.00'>{{d.rfidCode}}</div>
      </div>
    </div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="二维码位置(x)" prop="codexy">
            <el-input v-model="form.codex" placeholder="请输入资产编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="二维码位置(y)" prop="codexy">
            <el-input v-model="form.codey" placeholder="请输入资产编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="二维码大小" prop="codeSize">
            <el-input v-model="form.codeSize" placeholder="请输入资产编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="文本位置x" prop="textX">
            <el-input v-model="form.textX" placeholder="请输入资产编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="文本位置y" prop="textY">
            <el-input v-model="form.textY" placeholder="请输入资产编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="文本间隔" prop="textLineheight">
            <el-input v-model="form.textLineheight" placeholder="请输入资产编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字体宽" prop="textW">
            <el-input v-model="form.textW" placeholder="请输入资产编码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字体高" prop="textH">
            <el-input v-model="form.textH" placeholder="请输入资产编码"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  props: ['visible', 'title', 'assets'],
  data() {
    return {
      printEquips: [],
      filedSort: [
        {key: 'assetName', value: '', name: '资产名称'},
        {key: 'assetCode', value: '', name: '资产编码'},
        {key: 'brandName', value: '', name: '品牌'},
        {key: 'modelName', value: '', name: '型号'},
        {key: 'deptName', value: '', name: '使用部门'},
      ],
      form: {
        codex: 0.2,
        codey: 0.2,
        codeSize: 1,
        textX: 1.3,
        textY: 0.3,
        textLineheight: 0.3,
        textW: 10,
        textH: 40,
      },
      rules: {},
    }
  },
  watch: {
    visible(val) {
      if (val) {}
    }
  },
  mounted() {
    // this.init()
  },
  methods: {
    init() {
      DSInitService();
      web_Wait(1000, (res) => {
        if (!res) {
          DSLaunchApp();
          this.$modal.msgError("服务未启动，将启动服务；");
        }
        // GetPrinter();
        web_DSEnumPrinter(this.callBackFunGetPrinter)
      })

    },
    // 获取和选择打印机
    callBackFunGetPrinter(retVal) {
      if (retVal.RtnVal == null) {
        this.$modal.msgError("未找到打印机")
        return;
      }

      if (retVal.RtnCode == 0) {
        //output("成功枚举" + retVal.RtnVal.length + "台打印机！");
        var port = null;
        if (retVal.RtnVal.length == 1) { // 单台打印机
          port = retVal.RtnVal[0].substring(retVal.RtnVal[0].indexOf("@") + 1);
          this.beginWork(port);
        }
        else {
          var printerArray = []
          var printBtns = [];
          for (i in retVal.RtnVal) {
            var temp = retVal.RtnVal[i];
            printerName = temp.substring(0, temp.indexOf("@"));
            printerPort = temp.substring(temp.indexOf("@") + 1);
            printerArray.push({ "printerName": printerName, "printerPort": printerPort });
            printBtns.push(printerName + " 端口：" + printerPort);
          }
          this.printEquips = printerArray
        }

      }

    },
    // 打开打印机
    beginWork(port) {
      web_DSOpenPrinter(port, callBackFunOpenPrinter);
      let callBackFunOpenPrinter = (retVal) => {
        if (console) {
          console.log("打开打印机：" + retVal.RtnVal);
        }
        // printOpen = true;打开成功
        // this.assets.forEach(item => {
        //   this.writeEPC(item)
        // })
      }
      setTimeout(() => {
        this.assets.forEach(item => {
          this.writeEPC(item)
        })
      }, 1000)
    },
    // 写入epc
    writeEPC(row) {
      let getPrintEPCState = (retVal) => {
        var tem = retVal.RtnVal[0];
        if (tem == 0) {
          let epc = row.rfidCode;
          console.log("状态正常，准备写入" + epc);

          web_DAS_RFIDTag_Write_EPCIDA(epc, (retVal) => {
            if (retVal.RtnVal == 1) {
              console.log("EPC写入成功，发送打印数据");
              this.printexec(row); // 打印
              // 获取状态回调（打印后）
              web_DSGetStarPrint((retVal) => {
                var tem = retVal.RtnVal[0];
                if (tem == 0) {
                  console.log("再次获取状态成功");
                  // web_DSGetStarPrint(getprintStatePrintAfterAgain); // 再次获取状态
                }
                else {
                  console.log("再次获取状态失败 ");
                }
              }); // 再次获取状态
            }
            else {
              console.log("EPC写入失败，重新获取状态");
              web_DSGetStarPrint(getPrintEPCState); // 获取状态
            }
          }); // 写EPC
        }
        else if (tem == 1) { this.$modal.msgWarning("获取状态失败"); }
        else if (tem == 2) { this.$modal.msgWarning("卡纸出错"); }
        else if (tem == 3) { this.$modal.msgWarning("缺纸"); }
        else if (tem == 4) { this.$modal.msgWarning("碳带出错"); }
        else if (tem == 5) { this.$modal.msgWarning("打印头抬起"); }
        else if (tem == 6) { this.$modal.msgWarning("打印机暂停"); }
      }

      web_DSGetStarPrint(getPrintEPCState)
    },
    // 写入文本和二维码
    printexec(row) {
      web_DSZPLPrintText(2, 0, 0, 0, 0, 0, " ");
      this.printQrCode(parseFloat(this.form.codex), parseFloat(this.form.codey), this.form.codeSize, "https://asset.appmans.cn/giai/"+row.rfidCode+"?orgcode=001999998", 30, 2)
      this.filedSort.forEach((field,i) => {
        let text = field.name+'：'+row[field.key]
        this.printText(30, parseFloat(this.form.textX), parseFloat(this.form.textY+(this.form.textLineheight*i)), this.form.textW, this.form.textH, text, '宋体');
      })
      web_DSZPLPrintText(3, 0, 0, 0, 0, 0, " ");
    },
    // 写二维码
    printQrCode(XposQrCode,
                YposQrCode,
                sizeQrCode,
                textQrCode,
                byDarknessQrCode,//打印深度
                ErrLevelQrCode//容错级别) 
    ) {
      web_DSZPLPrintQrCodeExA(0, parseInt(byDarknessQrCode), parseFloat(XposQrCode), parseFloat(YposQrCode), parseFloat(sizeQrCode), parseInt(ErrLevelQrCode), textQrCode);
    },
    // 写文本
    printText(byDarkness, dXPos, dYpos, widthSizeFont,heightSizeFont,content,font) {
      web_DSZPLPrintTextEx(0, font, false, false, parseFloat(byDarkness), parseFloat(dXPos), parseFloat(dYpos), parseFloat(heightSizeFont), parseFloat(widthSizeFont), content);
    },
  },
};
</script>

<style lang="scss" scoped></style>
 